package com.isunland.managesystem.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.FunctionMenuInfo;
import java.util.List;

public class FunctionMenuAdapter extends ArrayAdapter<FunctionMenuInfo> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-05-26 14:07:17 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final RelativeLayout rootView;
    public final ImageView mFunctionImage;
    public final TextView mFunctionName;
    public final TextView mHintNumber;

    private ViewHolder(RelativeLayout rootView, ImageView mFunctionImage, TextView mFunctionName,
        TextView mHintNumber) {
      this.rootView = rootView;
      this.mFunctionImage = mFunctionImage;
      this.mFunctionName = mFunctionName;
      this.mHintNumber = mHintNumber;
    }

    public static ViewHolder create(RelativeLayout rootView) {
      ImageView mFunctionImage = (ImageView) rootView.findViewById(R.id.function_image);
      TextView mFunctionName = (TextView) rootView.findViewById(R.id.function_name);
      TextView mHintNumber =
          (TextView) rootView.findViewById(R.id.tv_hintNumber_functionMenuAdapter);
      return new ViewHolder(rootView, mFunctionImage, mFunctionName, mHintNumber);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_function_menu, parent, false);
      vh = ViewHolder.create((RelativeLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    FunctionMenuInfo item = getItem(position);
    if (item.getPic() != 0) {
      vh.mFunctionImage.setImageResource(item.getPic());
    } else {
      vh.mFunctionName.setTextSize(16);
    }
    if (item.getBg() != 0) {
      vh.rootView.setBackgroundResource(item.getBg());
    }
    if (item.getName() != 0) {

      vh.mFunctionName.setText(item.getName());
    }

    if (item.getNumber() != 0) {
      vh.mHintNumber.setVisibility(View.VISIBLE);
      vh.mHintNumber.setText(item.getNumber() + "");
    } else {
      vh.mHintNumber.setVisibility(View.GONE);
    }
    return vh.rootView;
  }

  private LayoutInflater mInflater;

  // Constructors
  public FunctionMenuAdapter(Context context, List<FunctionMenuInfo> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  public FunctionMenuAdapter(Context context, FunctionMenuInfo[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }
}
